package com.liu.util;

import com.liu.common.utils.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class MyRestTemplateUtil {

    /**
     *  post请求通过表单的方式
     * @param restTemplate
     * @param url
     * @param bodyParams
     * @param uriParams
     * @return
     */
    public static String postForFrom(RestTemplate restTemplate, String url, MultiValueMap bodyParams, Map uriParams){

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity request = new HttpEntity(bodyParams,headers);
          log.info("请求参数体"+ JacksonUtil.printJson(request));

        return restTemplate.exchange(url, HttpMethod.POST,request,String.class,uriParams).getBody().toString();


    }


    /**
     * 混合类型的post提交multpart
     * @param restTemplate
     * @param url
     * @param bodyParams
     * @param uriParams
     * @return
     */
    public static String postForMULTIPARTFrom(RestTemplate restTemplate, String url, MultiValueMap bodyParams, Map uriParams){

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity request = new HttpEntity(bodyParams,headers);
        log.info("请求参数体"+ JacksonUtil.printJson(request));


        return (String) restTemplate.exchange(url, HttpMethod.POST,request,String.class,uriParams).getBody();


    }


    public static String postForFile(RestTemplate restTemplate, String url, File file, Map uriParams){

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);


        FileSystemResource resource = new FileSystemResource(file);
        HttpEntity<FileSystemResource> request = new HttpEntity<FileSystemResource>(resource);
        log.info("请求参数体"+ JacksonUtil.printJson(request));


        return (String) restTemplate.exchange(url, HttpMethod.POST,request,String.class,uriParams).getBody();


    }

    public static String getForJson(RestTemplate restTemplate, String url, MultiValueMap bodyParams, Map uriParams){

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity request = new HttpEntity(bodyParams,headers);
        log.info("请求参数体"+ JacksonUtil.printJson(request));

        return (String) restTemplate.exchange(url, HttpMethod.GET,request,String.class,uriParams).getBody();


    }
    public static String getForJson(RestTemplate restTemplate, String url){

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity request = new HttpEntity(new LinkedMultiValueMap<>(),headers);
        log.info("请求参数体"+ JacksonUtil.printJson(request));

        return restTemplate.exchange(url, HttpMethod.GET,request,String.class,new HashMap<>()).getBody();


    }



}
